<?php

class UserLoginWidget extends CWidget {

    public $title = 'User login';
    public $visible = true;

    public function init() {
        if ($this->visible) {
			            
        }
    }

    public function run() {
        if ($this->visible) {
            $this->renderContent();
        }
    }

    protected function renderContent() {
    	$form=new LoginForm;
    	
    	// if it is ajax validation request
    	if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    	{
    		echo CActiveForm::validate($form);
    		Yii::app()->end();
    	}
    	
    	// collect user input data
    	if(isset($_POST['LoginForm']))
    	{
    			if (!isset($_POST['LoginForm']["check"])){
    				$form->attributes=$_POST['LoginForm'];
    				// validate user input and redirect to the previous page if valid
    				if($form->validate() && $form->login()) {
    					//$this->controller->redirect(Yii::app()->user->returnUrl);
						$getID = UserProfiles::model()->FindByAttributes(array('user_id'=>Yii::app()->user->id));
						header('Location: index.php?r=userProfiles/view&id='.$getID->id);
					}
    			}
       			    		
    	}
        $this->render('userLogin', array('form' => $form));
    }

}

?>
